/**
 * 2015年7月29日上午9:48:51
 * FuelConsumptionAlarmJob.javaFuelConsumptionAlarmJob
 */
package com.service.scheduler.job;

import java.text.DecimalFormat;
import java.util.List;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

import com.service.bean.device.Vehicle;
import com.service.bean.signal.GpsSignal;
import com.service.bean.alarm.AlarmInfo;
import com.service.cache.CacheManager;
import com.service.db.impl.DbOther;
import com.service.db.impl.DbSignal;
import com.service.other.Logger;

/**
 * 2015年7月29日上午9:48:51
 *
 * 张
 */
@SuppressWarnings("deprecation")
public class FuelConsumptionAlarmJob implements StatefulJob{
	public FuelConsumptionAlarmJob(){}
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			double oilPercent = 0;
			DecimalFormat df = new DecimalFormat("#0.00"); 
//			DbManager.instance().dbc.initVehicleMap();
			if (null == CacheManager.instance().vehicleMap || 0 == CacheManager.instance().vehicleMap.size()) {
				CacheManager.instance();
			}
			for(Vehicle equipment : CacheManager.instance().vehicleMap.values() ){
				List<GpsSignal> gpsList = DbSignal.getGpsBeanList(equipment.equipmentId,equipment.company);
				if(gpsList.size() > 1){
					//最后一条记录的油位值-第一条记录油位值+当天的加油量
					double changOil = gpsList.get(gpsList.size()-1).oilAmt-gpsList.get(0).oilAmt+DbOther.getCountOil(equipment.equipmentId);
					//当天的里程
					double todaymile = gpsList.get(gpsList.size()-1).todayMile;
					if(changOil != 0 && todaymile != 0){
						oilPercent = Math.abs(changOil)*100/todaymile;
						if(equipment.normalOilValue != 0 && oilPercent > equipment.normalOilValue){
							AlarmInfo alarm = new AlarmInfo();
							alarm.signalId = gpsList.get(0).equipmentId;
							alarm.gpsNo = gpsList.get(0).gpsNo;
							alarm.equipmentId = equipment.equipmentId;
							alarm.alarmLocation = gpsList.get(0).baiduLocation;
							alarm.companyCode = gpsList.get(0).companyCode;
							alarm.alarmMessage = "百公里油耗值为"+df.format(oilPercent)+",超过正常值:"+equipment.normalOilValue+",已经超标!";
							//将数据插入到报警表中
							//DbOther.insertAlarm(alarm);
							CacheManager.instance().getAlarmSaveQueue(equipment.company).offer(alarm);
						}
					}
				}
			}
		} catch (Exception e) {
			Logger.err(e);
		}
	}

}
